package com.simoniu.atm.app;

import com.simoniu.atm.domain.Account;
import com.simoniu.atm.service.AccountService;
import java.util.Scanner;

public class AtmDemo {

    public static Scanner sc = new Scanner(System.in);
    public static int choice;
    public static AccountService accountService = new AccountService();
    public static Account currentAccount = null; //当前的银行账号

    //主界面
    public static void showMenu() {
        System.out.println("**************XXX银行ATM系统**************");
        System.out.println("1.开户");
        System.out.println("2.存款");
        System.out.println("3.取款");
        System.out.println("4.转账");
        System.out.println("5.退出系统");
        System.out.println("************************************");
        System.out.println("请选择(1-5):");
        choice = sc.nextInt();
    }

    public static void main(String[] args) {

        showMenu();

        while (true) {
            if (choice == 5) {
                break;
            }
            switch (choice) {
                case 1:
                    if ((currentAccount = accountService.createAccount()) != null) {
                        System.out.println("恭喜您，开户成功！");
                        System.out.println(currentAccount);
                    }
                    break;
                case 2:
                    //System.out.println("执行存款...");
                    //1.第一步登录。
                    System.out.println("请输入银行卡号： ");
                    String cid = sc.next();
                    System.out.println("请输入密码：");
                    String password = sc.next();
                    if ((currentAccount = accountService.login(cid, password)) != null) {
                        System.out.println("请输入存款金额：");
                        int money = sc.nextInt();

                        if (accountService.deposit(currentAccount, money)) {
                            System.out.println("存款成功！");
                            //查询一下最新的余额
                            int balance = accountService.queryBalanceByCid(currentAccount.getCid());
                            System.out.println("当前余额是：" + balance);
                        } else {
                            System.out.println("存款失败！");
                        }

                    } else {
                        System.out.println("该账号不存在！");
                    }
                    break;
                case 3:
                    //1.第一步登录。
                    System.out.println("请输入银行卡号： ");
                    cid = sc.next();
                    System.out.println("请输入密码：");
                    password = sc.next();
                    if ((currentAccount = accountService.login(cid, password)) != null) {
                        System.out.println("请输入取款金额：");
                        int money = sc.nextInt();
                        if (accountService.withdraw(currentAccount, money)) {
                            System.out.println("取款成功！");
                            //查询一下最新的余额
                            int balance = accountService.queryBalanceByCid(currentAccount.getCid());
                            System.out.println("当前余额是：" + balance);
                        } else {
                            System.out.println("取款失败！");
                        }
                    } else {
                        System.out.println("该账号不存在！");
                    }
                    break;
                case 4:
                    //1.第一步登录。
                    System.out.println("请输入银行卡号： ");
                    cid = sc.next();
                    System.out.println("请输入密码：");
                    password = sc.next();
                    if((currentAccount = accountService.login(cid,password))!=null){
                        System.out.println("请输入转账卡号：");
                        String destId = sc.next();
                        if (accountService.checkAccountIdExists(destId)) {
                            System.out.println("请输入转账金额:");
                            int money = sc.nextInt();
                            if (accountService.transfer(cid, destId, money)) {
                                System.out.println("转账成功!");
                                System.out.println("存款余额是：" + accountService.queryBalanceByCid(cid) + " 元");
                            } else {
                                System.out.println("转账失败!");
                            }
                        }else{
                            System.out.println("转账卡号不存在！");
                        }
                    }else{
                        System.out.println("该账号不存在！");
                    }
                    break;
            }
            showMenu();
        }
        System.out.println("感谢您的使用，byebye!");
    }
}
